﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
using WebBHXH.Entities;
using System.IO;

namespace WebBHXH
{
    public partial class About : System.Web.UI.Page
    {
        List<NguoiSudungModel> listNguoiSudungModel;
        List<MucBHModel> listMucBHModel;
        List<MaDotBHModel> listMaDotBHModel;
        List<CoSoKCBModel> listCoSoKCBModel;
        List<LoaiBHModel> listLoaiBHModel;
        string id = "0";
        protected void Page_Load(object sender, EventArgs e)
        {

            GetDotBH();
            GetCoSoKCB();
            GetLoaiBHModel();
            GetMucBHModel();
            GetNguoiSudungModel();
            if (!IsPostBack)
            {
                
                fillDropdown();
                if (Request.QueryString["mode"] == "add" || Request.QueryString["Id"] == null)
                {
                    if (Request.QueryString["Madot"] != null)
                    {
                        MaDotBHModel maDotBHModel = new MaDotBHModel().GetDotBHById(Request.QueryString["Madot"]);
                        hdfId.Value = maDotBHModel.MaDotBH;                        
                        DropDownList1.SelectedIndex = listMaDotBHModel.FindIndex(delegate(MaDotBHModel maDotBH)
                        {
                            return maDotBH.MaDotBH.Equals(maDotBHModel.MaDotBH, StringComparison.Ordinal);
                        }) + 1;

                        DropDownList6.SelectedIndex = listLoaiBHModel.FindIndex(delegate(LoaiBHModel loaiBHModel)
                        {
                            return loaiBHModel.MaLoaiBH.Equals(maDotBHModel.LoaiBH, StringComparison.Ordinal);
                        }) + 1;

                        DropDownList7.SelectedIndex = listMucBHModel.FindIndex(delegate(MucBHModel mucBHModel)
                        {
                            return mucBHModel.MaMucBH.Equals(maDotBHModel.MucBH, StringComparison.Ordinal);
                        }) + 1;
                    }
                    txtId.Visible = false;
                    Label1.Visible = false;
                    btnDangKy.Text = "Đăng Ký";
                }
                else
                {
                    if (Request.QueryString["Id"] != null)
                    {
                        id = Request["Id"];
                        MaBHYTTNModel maBHYTTNModel = new MaBHYTTNModel().GetBHYTTNById(id);
                        txtName.Text = maBHYTTNModel.HoVaTen;
                        txtAddress.Text = maBHYTTNModel.DiaChi;
                        dpkFrom.SelectedDate = maBHYTTNModel.TuNgay;
                        dpkTo.SelectedDate = maBHYTTNModel.DenNgay;
                        txtNote.Text = maBHYTTNModel.GhiChu;
                        if (maBHYTTNModel.GioiTinh == "Nam")
                        {
                            rbtnNam.Checked = true;
                        }
                        else
                        {
                            rbtnNu.Checked = true;
                        }

                        DropDownList1.SelectedIndex = listMaDotBHModel.FindIndex(delegate(MaDotBHModel maDotBHModel)
                        {
                            return maDotBHModel.MaDotBH.Equals(maBHYTTNModel.MaDotBH, StringComparison.Ordinal);
                        }) + 1;

                        DropDownList5.SelectedIndex = listCoSoKCBModel.FindIndex(delegate(CoSoKCBModel coSoKCBModel)
                        {
                            return coSoKCBModel.MaCoSoKCB.Equals(maBHYTTNModel.CoSoKCB, StringComparison.Ordinal);
                        }) + 1;

                        DropDownList6.SelectedIndex = listLoaiBHModel.FindIndex(delegate(LoaiBHModel loaiBHModel)
                        {
                            return loaiBHModel.MaLoaiBH.Equals(maBHYTTNModel.LoaiBH, StringComparison.Ordinal);
                        }) + 1;

                        DropDownList7.SelectedIndex = listMucBHModel.FindIndex(delegate(MucBHModel mucBHModel)
                        {
                            return mucBHModel.MaMucBH.Equals(maBHYTTNModel.MucBH, StringComparison.Ordinal);
                        }) + 1;

                        ddlDate.SelectedValue = maBHYTTNModel.NgaySinh.Day.ToString();
                        ddlMonth.SelectedValue = maBHYTTNModel.NgaySinh.Month.ToString();
                        ddlYear.SelectedValue = maBHYTTNModel.NgaySinh.Year.ToString();

                        btnDangKy.Text = "Cập Nhật";
                    }
                }
                txtId.Text = id;

            }
        }

        public void fillDropdown() {
          
            DropDownList7.Items.Add("-- Chọn Mục BH --");
            foreach (MucBHModel mucBHModel in listMucBHModel)
            {
                DropDownList7.Items.Add(mucBHModel.TenMucBH);
            }
            DropDownList5.Items.Add("-- Chọn Cơ Sở --");
            foreach (CoSoKCBModel coSoKCBModel in listCoSoKCBModel)
            {
                DropDownList5.Items.Add(coSoKCBModel.Ten_CoSoKCB);
            }
            DropDownList6.Items.Add("-- Chọn Loại BH --");
            foreach (LoaiBHModel loaiBHModel in listLoaiBHModel)
            {
                DropDownList6.Items.Add(loaiBHModel.TenLoaiBH);
            }
            DropDownList1.Items.Add("-- Chọn Đợt --");
            foreach (MaDotBHModel maDotBH in listMaDotBHModel)
            {
                DropDownList1.Items.Add(maDotBH.MaDotBH);
            }

            GetBirthDay();
            
        }
        
        private void GetBirthDay()
        {
            ddlDate.Items.Add("Ngày:");
            for (int i = 1; i <= 31; i++)
            {
                ddlDate.Items.Add(i.ToString());    
            }
            ddlMonth.Items.Add("Tháng:");
            for (int i = 1; i <= 12; i++)
            {
                ddlMonth.Items.Add(i.ToString());
            }
            ddlYear.Items.Add("Năm:");
            for (int i = 1900; i < DateTime.Now.Year; i++)
            {
                ddlYear.Items.Add(i.ToString());
            }
        }

        private void GetNguoiSudungModel()
        {
            listNguoiSudungModel = new NguoiSudungModel().GetNguoiSudungModel();
        }

        private void GetMucBHModel()
        {
            listMucBHModel = new MucBHModel().GetMucBHModel();
        }

        private void GetDotBH()
        {
            listMaDotBHModel = new MaDotBHModel().GetDotBH();
        }

        private void GetCoSoKCB()
        {
            listCoSoKCBModel = new CoSoKCBModel().GetCoSoKCB();
        }

        private void GetLoaiBHModel()
        {
            listLoaiBHModel = new LoaiBHModel().GetLoaiBHModel();
        }
        protected void btnDangKy_Click(object sender, EventArgs e)
        {
            string maDotBaoHiem = DropDownList1.SelectedValue;
            string hoTen = txtName.Text;
            string ngaySinh = ddlDate.SelectedValue + "/" + ddlMonth.SelectedValue + "/" + ddlYear.SelectedValue;
            string gioiTinh = (rbtnNam.Checked) ? "Nam" : "Nữ";
            string diaChi = txtAddress.Text;
            string coSoKCB = listCoSoKCBModel[DropDownList5.SelectedIndex - 1].MaCoSoKCB;
            string tuNgay = dpkFrom.SelectedDate.ToShortDateString();
            string denNgay = dpkTo.SelectedDate.ToShortDateString();
            string loaiBH = listLoaiBHModel[DropDownList6.SelectedIndex - 1].MaLoaiBH;
            string mucBH = listMucBHModel[DropDownList7.SelectedIndex - 1].MaMucBH;
            string maNguoiDung = Session["id"].ToString();
            string ghiChu = txtNote.Text;
            hdfId.Value = maDotBaoHiem;
            System.Net.WebRequest request = null;
            if (txtId.Text == "0")
            {
                request = System.Net.WebRequest.Create(""+Util.getUrl() + "Home/insertBHYTTN?MaDotBH=" + maDotBaoHiem + "&HoVaTen=" + hoTen + "&NgaySinh=" + ngaySinh + "&GioiTinh=" + gioiTinh + "&DiaChi=" + diaChi + "&CoSoKCB=" + coSoKCB + "&TuNgay=" + tuNgay + "&DenNgay=" + denNgay + "&LoaiBH=" + loaiBH + "&MucBH=" + mucBH + "&NSD=" + maNguoiDung + "&GhiChu=" + ghiChu);
            }
            else
            {
                request = System.Net.WebRequest.Create(""+Util.getUrl() + "Home/updateBHYTTN?MaBHYTTN=" + txtId.Text + "&MaDotBH=" + maDotBaoHiem + "&HoVaTen=" + hoTen + "&NgaySinh=" + ngaySinh + "&GioiTinh=" + gioiTinh + "&DiaChi=" + diaChi + "&CoSoKCB=" + coSoKCB + "&TuNgay=" + tuNgay + "&DenNgay=" + denNgay + "&LoaiBH=" + loaiBH + "&MucBH=" + mucBH + "&GhiChu=" + ghiChu + "&NSD=" + maNguoiDung);
            }
            request.Method = "POST";
            request.ContentLength = 0;
            request.ContentType = "application/json; charset=utf-8";
            //get response-stream, and use a streamReader to read the content
            Stream s = request.GetResponse().GetResponseStream();
            StreamReader sr = new StreamReader(s);
            string result = sr.ReadToEnd();
            if (result == "200")
            {
                //Label1.Text = "ok";
            }
            else if (result == "2001")
            {
                //Label1.Text = "thanhtq";
            }

            txtNote.Text = "";
            txtName.Text = "";
            ddlDate.SelectedIndex = 0;
            ddlMonth.SelectedIndex = 0;
            ddlYear.SelectedIndex = 0;
            //Response.Redirect("Default.aspx?Id=" + id);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("DanhSachBHTN.aspx?Id=" + hdfId.Value);
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            MaDotBHModel maDotBHModel = new MaDotBHModel().GetDotBHById(DropDownList1.SelectedValue);
            DropDownList6.SelectedIndex = listLoaiBHModel.FindIndex(delegate(LoaiBHModel loaiBHModel)
            {
                return loaiBHModel.MaLoaiBH.Equals(maDotBHModel.LoaiBH, StringComparison.Ordinal);
            }) + 1;

            DropDownList7.SelectedIndex = listMucBHModel.FindIndex(delegate(MucBHModel mucBHModel)
            {
                return mucBHModel.MaMucBH.Equals(maDotBHModel.MucBH, StringComparison.Ordinal);
            }) + 1;
        }
    }
}
